package com.tfswx.jcyw;

import javazoom.jl.player.Player;

import java.io.*;

/**
 * @author ricky
 * @date 2022/7/28
 */
public class MusicPlayer {
    private static Thread thread;

    public static void play(File file) {
        try {
            FileInputStream stream = new FileInputStream(file);
            play(stream);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("播放失败");
        }
    }

    public static void play(InputStream is) {
        if (thread != null) {
            thread.stop();
        }
        thread = new Thread(() -> {
            try {
                Player player = new Player(is);
                player.play();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                thread = null;
            }
        });
        thread.start();
    }

}
